/*
    该算法时间复杂度一般为o(M)最差为o(m*n)
    也可以解决边权位正的问题
    算法思路也是便边不过我们之便利节点跟新过的边
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int N = 1e5 + 10;
int n, m;
int dis[N];
queue<int> q;
bool st[N];
struct edge
{
    int w;
    int to;
    int next;
}edges[N];
int ind;
int head[N];
void add(int u, int v, int w)
{
    edges[ind].w = w;
    edges[ind].to = v;
    edges[ind].next = head[u];
    head[u] = ind++;
}
bool spfa()
{
    memset(dis, 0x3f, sizeof(dis));
    q.push(1);
    st[1] = true;
    dis[1]=0;
    
    while (q.size())
    {
        int tem = q.front();
        q.pop();
        st[tem]=false;//如果该点下次还被跟新就可以在进行入队
        for (int i = head[tem]; i != -1; i = edges[i].next)
        {
            int j = edges[i].to;
            if (dis[tem] + edges[i].w < dis[j])
            {
                dis[j] = dis[tem] + edges[i].w;
                if (!st[j])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }

    if (dis[n] == 0x3f3f3f3f3f3f3f3f) return false;
    else return true;
}
signed main()
{
    memset(head, -1, sizeof(head));
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        add(u, v, w);
    }
    if (spfa()) cout << dis[n];
    else cout << "impossible";
}